package MyString;

public class Leetcode1653 {
    public static int minimumDeletions(String s) {
        int len = s.length();
        if(len < 2){
            return 0;
        }
        int[] left = new int[len];
        int[] right = new int[len];
        // 左边b的数目
        int count1 = 0, count2 =0, flag = 0, flag2 =0;

        for(int i=0; i<len; i++){
            if(s.charAt(i) == 'b'){
                left[i] = ++count1;
                flag = 1;
            }else{
                left[i] = count1;
            }
        }

        for(int ii : left){
            System.out.print(ii + " ");
        }
        System.out.println();
        // 右边a的数目
        for(int i=len-1; i>=0; i--){
            if(s.charAt(i) == 'a'){
                right[i] = ++count2;
                flag2 =1;
            }else{
                right[i] = count2;
            }
        }
        for(int ii : right){
            System.out.print(ii + " ");
        }
        System.out.println();
        if(flag + flag2 < 2){
            return 0;
        }

        int maxn = 100001;
        for(int i=-1;i<=len-1;i++){
            if(i == len -1){
                maxn = Math.min(left[i], maxn);
            }else if(i == -1){
                maxn = Math.min(right[i+1], maxn);
            }else{
                maxn = Math.min(left[i] + right[i+1], maxn);
            }
        }

        return maxn;
    }

    public static void main(String[] args) {
        String aa = "bbbbbbbaabbbbbaaabbbabbbbaabbbbbbaabbaaabaabbbaaaabaaababbbabbabbaaaabbbabbbbbaabbababbbaaaaaababaaababaabbabbbaaaabbbbbabbabaaaabbbaba";
        System.out.println(minimumDeletions(aa));
    }
}
